package com.yunkecn.transactiondemo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("user")
public class UserController {
    
    @Autowired
    UserService userService;
    
    @RequestMapping("info")
    @ResponseBody
    public Map<String, String> getUserInfo(String userId) {
        String name = userService.getUserName(userId);
        Map<String, String> result = new HashMap<>(1);
        result.put("name", name);
        return result;
    }
    
    @RequestMapping("update")
    @ResponseBody
    public String updateUserName(String userId, String name) {
        int result = userService.updateUserName(userId, name);
        userService.updateAge(userId);
        if (result == 1) {
            return "success";
        }
        return "failed";
    }
    
    @RequestMapping("updateLong")
    @ResponseBody
    public String updateAgeLongTime(String userId) {
        if (userService.updateAgeUseLongTime(userId) == 1) {
            return "success";
        }
        return "failed";
    }
    
    @RequestMapping("insertLong")
    @ResponseBody
    public String addUserLongTime(String userId) {
        if (userService.addUserUseLongTime(userId) >= 1) {
            return "success";
        }
        return "failed";
    }
    
}
